package com.ruoyi.web.controller.system;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.publish.domain.Article;
import com.ruoyi.publish.domain.UpdateStatusGroup;
import com.ruoyi.publish.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/publish/article")
public class ArticleController extends BaseController {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/list")
    public TableDataInfo selectList(Article article){
        startPage();
        List<Article> articleList=articleService.selectList(article);
        System.out.println("查询文章数量："+articleList.size());
        return getDataTable(articleList);
    }
//   获得文章详情
    @GetMapping("/{articleId}")
    public AjaxResult selectById(@PathVariable Long articleId){
        return AjaxResult.success(articleService.selectArticleById(articleId));
    }

    @PostMapping("/add")
    public AjaxResult insertArticle(@RequestBody Article article){
        //1.校验文章名是否唯一
        if (!articleService.checkAticleNameUnique(article)) {
            return error("新增文章'" + article.getArticleName() + "'失败，文章名称已存在");
        }
        //校验栏目是否存在
//        Long columnId = article.getColumnId();
//        if (columnId == null || !articleService.checkColumnExists(columnId)) {
//            return error("新增文章" + article.getArticleName() + "失败，栏目不存在");
//        }
        article.setCreateBy(SecurityUtils.getUsername());
        return  AjaxResult.success(articleService.insertArticle(article));
    }

    /**
     *
     * @param file
     * @return 文件路径
     * @throws IOException
     */
    @Anonymous
    @PostMapping("/upload")
    public AjaxResult insertImage(@RequestParam MultipartFile file) throws IOException {
        System.out.println("上传文件");
        String path=FileUploadUtils.upload(RuoYiConfig.getUploadPath(),file);
        return AjaxResult.success("上传成功",path);
    }
    @PutMapping("/edit")
    public AjaxResult updateArticle(@RequestBody Article article){
        article.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(articleService.updateArticle(article));
    }
    @DeleteMapping("/{articleId}")
    public AjaxResult deleteArticle(@PathVariable Long articleId ){
        //检验文章存在
        if(!articleService.checkArticleExists(articleId)){
            return error("删除文章失败，文章不存在");
        }
        return toAjax(articleService.deleteArticleById(articleId));
    }
    //状态  发布和撤回
    @Log(title = "文章管理",businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody @Validated(UpdateStatusGroup.class) Article article){
        article.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(articleService.updateStatus(article));
    }
    //查询栏目名
    @GetMapping("/columnName")
    public AjaxResult selectColumn(){
        return AjaxResult.success(articleService.selectColumnName());
    }
   //查询文章是否已推荐，推荐就不能退回
    @GetMapping("/checkCanRecall/{articleId}")
    public AjaxResult checkArticleRecommended(@PathVariable Long articleId) {
        boolean canRecall  = !articleService.checkArticleCanRecall(articleId);
        return AjaxResult.success(canRecall);
    }


}
